home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Sample Controls / BDiamond / CBaseEventServer.cp < prev    next >
Encoding:
Text File  |  1996-12-20  |  4.2 KB  |  172 lines  |  [TEXT/CWIE]

  1. #include "ocheaders.h"
  2. #include "BDDISPIDs.h"
  3. #include "CBaseControl.h"
  4. #include "CBaseEventServer.h"
  5. #include "BDUtils.h"
  6. #include "BDAssert.h"
  7. #include "FnAssert.h"
  8. #include "dispatch.h"
  9. #include <LArray.h>
  10. #include <LArrayIterator.h>
  11. #include "CConnectionPoint.h"
  12. #include "CCPContainer.h"
  13.  
  14. ///////////////////////////////////////////////////////////////////////////////
  15. //
  16. // Constants
  17. //
  18.  
  19. ///////////////////////////////////////////////////////////////////////////////
  20. //
  21. // class statics
  22. //
  23.  
  24. ///////////////////////////////////////////////////////////////////////////////
  25. //
  26. //  CBaseEventServer::CBaseEventServer
  27. //
  28. CBaseEventServer::CBaseEventServer(void)
  29. {
  30.     mOutgoingInterfaceID     = kUninitializedInterfaceID;
  31.     mCPContainerObj            = NULL;
  32.     mCPContainerP            = NULL;
  33. }
  34.  
  35. ///////////////////////////////////////////////////////////////////////////////
  36. //
  37. //  CBaseEventServer::~CBaseEventServer
  38. //
  39. CBaseEventServer::~CBaseEventServer()
  40. {
  41. }
  42.  
  43. ///////////////////////////////////////////////////////////////////////////////
  44. //
  45. //  CBaseControl::IUnknown::QueryInterface
  46. //
  47. //  Returns a pointer to the specified interface on a component to which a
  48. //  client currently holds an interface pointer.
  49. //
  50. STDMETHODIMP
  51. CBaseEventServer::QueryInterface(REFIID inRefID, void** outObj)
  52. {
  53.     ErrorCode    result = CBaseCOM::QueryInterface(inRefID, outObj);
  54.  
  55.     if ( result == E_NOINTERFACE )
  56.     {
  57.         void* pv = nil;
  58.  
  59.         if (inRefID == IID_IConnectionPointContainer) 
  60.         {
  61.             if ( mCPContainerP )
  62.                 return mCPContainerP->QueryInterface(IID_IConnectionPointContainer, outObj);
  63.         }
  64.  
  65.         *outObj = pv;
  66.  
  67.         // if we got an interface, ref it and return ok
  68.         if ( pv )
  69.         {
  70.              ((IUnknown*) pv)->AddRef();
  71.             result = S_OK;
  72.         }
  73.     }
  74.     
  75.     return result;
  76. }
  77.  
  78.  
  79. ///////////////////////////////////////////////////////////////////////////////
  80. //
  81. //  CBaseEventServer::AddOutGoingInterface
  82. //
  83. Boolean 
  84. CBaseEventServer::AddOutGoingInterface(IID outgoingInterfaceID)
  85. {
  86.     Boolean addedIt = false;
  87.     
  88.     // right now, we only support one outgoing interface.
  89.     assert(mOutgoingInterfaceID == kUninitializedInterfaceID);
  90.     mOutgoingInterfaceID = outgoingInterfaceID;
  91.     
  92.     addedIt = true;
  93.     
  94.     return addedIt;
  95. }
  96.  
  97. ///////////////////////////////////////////////////////////////////////////////
  98. //
  99. //  CBaseEventServer::SetUpConnectionPoints
  100. //
  101. void 
  102. CBaseEventServer::SetUpConnectionPoints(void)
  103. {
  104.     // MMF: It seems to me that all of this is a bit dangerous to do in
  105.     // a constructor the way the MS samples do, so we do it here.  The
  106.     // superclass can call it whenever it wants.
  107.     
  108.     if ( ! mCPContainerObj )
  109.     {
  110.         // Create the new connection point container object
  111.         mCPContainerObj = new CCPContainer(NUM_EVENT_SERVER_CONNECTIONS);
  112.         
  113.         if ( mCPContainerObj )
  114.         {
  115.             // If we have a container object, allocate the connection points.  
  116.             // We currently only support one connection point.  
  117.             if ( mCPContainerObj->QueryInterface(IID_IConnectionPointContainer, &mCPContainerP) == S_OK
  118.                     && mCPContainerP )
  119.             {
  120.                 mCPContainerObj->AddConnectionPoint(mOutgoingInterfaceID);
  121.             }
  122.         }
  123.     }
  124. }
  125.  
  126.  
  127. ///////////////////////////////////////////////////////////////////////////////
  128. //
  129. //  CBaseEventServer::FireEvent
  130. //
  131.  
  132. STDMETHODIMP
  133. CBaseEventServer::FireEvent(REFIID refID, long eventID, PlatformEvent * event)
  134. {
  135.     if ( mCPContainerP )
  136.     {
  137.         IEnumConnectionPoints *    enumCP;
  138.         IEnumConnections *        enumC;
  139.         CONNECTDATA                connectData;
  140.         IUnknown *                eventTarget;
  141.         IConnectionPoint *        connectionPoint;
  142.         
  143.         // Get an enumerator for the connection points
  144.         if ( SUCCEEDED(mCPContainerP->EnumConnectionPoints(&enumCP)) )
  145.         {
  146.             // Loop through all the connection points for this control
  147.             while ( enumCP->Next(1, &connectionPoint, nil) == NOERROR )
  148.             {
  149.                 // Get all the connections for this connection point
  150.                 if ( SUCCEEDED( connectionPoint->EnumConnections(&enumC) ))
  151.                 {
  152.                     // Loop through all the connections for this connection point    
  153.                     while ( enumC->Next(1, &connectData, nil) == NOERROR )
  154.                     {
  155.                         // Get the interface implementation for this connection
  156.                         // if successful, fire the event
  157.                         if ( SUCCEEDED( connectData.pUnk->QueryInterface(refID, (void**) &eventTarget) ))
  158.                             FireOneEvent(refID, eventID, eventTarget, event);
  159.                     }
  160.                     
  161.                     // Release the enumerator
  162.                     enumC->Release();
  163.                 }
  164.             }
  165.             
  166.             enumCP->Release();
  167.         }
  168.     }
  169.     
  170.     return ResultFromScode(S_OK);
  171. }
  172.